<?php


namespace app\repository;



use app\Enums\GamesStatus;
use app\model\Games;
use app\model\Logs;
use app\Server\PushServer;
use app\Server\TcpServer;
use Carbon\Carbon;
use support\Cache;

class Repository
{

    public static function getCardNo()
    {
        $rand = mt_rand(0x00000000, 0xffffffff);// make sure we're not over 0xffffff, which shouldn't happen anyway
        $rand = dechex($rand & 0xffffffff);  // add zeroes in front of the generated string   //如果有0确保0在首位
        $rand = str_pad($rand, 8, '0', STR_PAD_LEFT);
        $var = Cache::get("card_no-list");
        if ($var){
            $list = json_decode($var);
            $in_array = in_array($rand, $list);
            $exists = Games::query()->where("device_id", $rand)->whereIn("status", [GamesStatus::END,GamesStatus::CANCEL])->exists();
            if ($in_array || $exists){
                $rand = self::getCardNo();
            }
            $list[] = $rand;
            //一天过期
            Cache::set('card_no-list',json_encode($list),1440);
            return $rand;
        }
        Cache::set('card_no-list',json_encode([$rand]),1440);

        return $rand;
    }
    public function getPushServer()
    {
        return new PushServer();
    }
    public function success($data = [], $msg = "操作成功",$code = 200)
    {
        return [
            "status" => true,
            "msg" => $msg,
            "data" => $data,
            "code" => $code
        ];

    }

    public function getTcpServer()
    {
        return new TcpServer();
    }



    public function ok($msg = "操作成功",$code = 200)
    {
        return [
            "status" => true,
            "msg" => $msg,
            "data" => [],
            "code"=>$code
        ];

    }

    public function error($msg = "", $code = 300)
    {
        return [
            "status" => false,
            "msg" => $msg,
            "code" => $code
        ];
    }

    public function event($channel, $input_data, $content, $message): array
    {
        return [
            "channel" => $channel,
            "input_data" => json_encode($input_data),
            "content" => json_encode($content),
            "message" => $message
        ];
    }

    public function integralRecordEvent($type,$userId,$operate_user_id,$order_sn,$price,$remark): array
    {
        return [
            "type"=>$type,
            "user_id"=>$userId,
            "operate_user_id"=>$operate_user_id,
            "order_sn"=>$order_sn,
            "price"=>$price,
            "remark"=>$remark
        ];
    }

    public function createLogs($channel, $input_data, $content, $message)
    {
        Logs::query()->create([
            "channel"=>$channel,
            "input_data"=>$input_data,
            "content"=>$content,
            "message"=>$message
        ]);
    }

    public static function create_uuid($prefix = "")
    {
        $chars = md5(uniqid(mt_rand(), true));
        $uuid = substr($chars, 0, 8) . '-'
            . substr($chars, 8, 4) . '-'
            . substr($chars, 12, 4) . '-'
            . substr($chars, 16, 4) . '-'
            . substr($chars, 20, 12);
        return $prefix . $uuid;
    }


    public static function create_order_sn()
    {
        return date("YmdHis") . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    }

}
